package com.sakura.serivce.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sakura.common.CustomerException;
import com.sakura.mapper.CategoryMapper;
import com.sakura.pojo.Category;
import com.sakura.pojo.Dish;
import com.sakura.pojo.Setmeal;
import com.sakura.serivce.CategoryService;
import com.sakura.serivce.DishService;
import com.sakura.serivce.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 删除菜品，并进行判断是否能删除
     *
     * @param id
     */
    @Override
    public void remove(Long id) {
        // 判断菜品分类，是否关联菜品
        QueryWrapper<Dish> dishQueryWrapper = new QueryWrapper<>();
        dishQueryWrapper.eq("category_id", id);
        int dishCount = dishService.count(dishQueryWrapper);
        if (dishCount > 0) {
            throw new CustomerException("菜品分类已有关联，不能删除");
        }

        // 判断套餐分类，是否关联菜品
        QueryWrapper<Setmeal> setmealQueryWrapper = new QueryWrapper<>();
        setmealQueryWrapper.eq("category_id", id);
        int setmealCount = setmealService.count(setmealQueryWrapper);
        if (setmealCount > 0) {
            throw new CustomerException("菜品分类已有关联，不能删除");
        }
        // 没有则删除
        this.removeById(id);
    }
}
